package com.whfc.ms.api.sys.controller;

import com.whfc.common.enums.SysRulePlatform;
import com.whfc.common.result.ListData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.dto.SysRuleDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.param.SysRuleAdd;
import com.whfc.fuum.param.SysRuleEdit;
import com.whfc.ms.api.sys.service.MsSysRuleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/7/23 20:51
 */
@RestController
@RequestMapping("/ms/api/rule")
public class SysRuleController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsSysRuleService msSysRuleService;

    @GetMapping("list")
    public Result getRuleList(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                              @RequestParam(value = "platform", required = false) Integer platform) {
        if (platform == null) {
            platform = SysRulePlatform.MS.getValue();
        }
        logger.info("权限管理-查询模块列表,loginUser:{},platform:{}", loginUser.getUsername(), platform);

        List<SysRuleDTO> list = msSysRuleService.getRuleList(loginUser, platform);
        return ResultUtil.success(new ListData<>(list));
    }

    @GetMapping("descendant/list")
    public Result getDescendantList(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                                    @RequestParam("pid") Integer pid) {
        logger.info("权限管理-查询子孙权限列表,loginUser:{},pid:{}", loginUser.getUsername(), pid);
        List<SysRuleDTO> list = msSysRuleService.getDescendantList(pid);
        return ResultUtil.success(new ListData<>(list));
    }

    @RequiresPermissions(value = {"sys/rule/add"})
    @PostMapping("add")
    public Result addRule(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                          @Validated @RequestBody SysRuleAdd params) {
        logger.info("权限管理-新增权限,loginUser:{},params:{}", loginUser.getUsername(), params);
        msSysRuleService.addRule(params);
        return ResultUtil.success();
    }

    @RequiresPermissions(value = {"sys/rule/edit"})
    @PostMapping("edit")
    public Result editRule(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                           @Validated @RequestBody SysRuleEdit params) {
        logger.info("权限管理-编辑权限,loginUser:{},params:{}", loginUser.getUsername(), params);
        msSysRuleService.editRule(params);
        return ResultUtil.success();
    }

    @RequiresPermissions(value = {"sys/rule/del"})
    @PostMapping("del/{ruleId}")
    public Result delRule(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                          @PathVariable("ruleId") Integer ruleId) {
        logger.info("权限管理-删除权限,loginUser:{},ruleId:{}", loginUser.getUsername(), ruleId);
        msSysRuleService.delRule(ruleId);
        return ResultUtil.success();
    }

    @RequiresPermissions(value = {"sys/rule/enable"})
    @PostMapping("enable/{ruleId}")
    public Result enableRule(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                             @PathVariable("ruleId") Integer ruleId) {
        logger.info("权限管理-启用权限,loginUser:{},ruleId:{}", loginUser.getUsername(), ruleId);
        msSysRuleService.enableRule(ruleId);
        return ResultUtil.success();
    }

    @RequiresPermissions(value = {"sys/rule/enable"})
    @PostMapping("disable/{ruleId}")
    public Result disableRule(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                              @PathVariable("ruleId") Integer ruleId) {
        logger.info("权限管理-禁用权限,loginUser:{},ruleId:{}", loginUser.getUsername(), ruleId);
        msSysRuleService.disableRule(ruleId);
        return ResultUtil.success();
    }

    @RequiresPermissions(value = {"sys/rule/edit"})
    @PostMapping("img")
    public Result ruleImg(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                          MultipartFile img) throws Exception {
        logger.info("权限管理-上传图片,loginUser:{},img:{}", loginUser.getUsername(), img.getOriginalFilename());
        OssPathDTO pathDTO = msSysRuleService.uploadImg(img);
        return ResultUtil.success(pathDTO);
    }
}
